package com.ricky.mq.client.session;

import cn.hutool.core.convert.Convert;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.ricky.mq.client.code.constant.ClientCommandCode;
import com.ricky.mq.client.code.handler.WaitOrSendMsgHandler;
import com.ricky.mq.client.message.BoringMessage;
import com.ricky.mq.client.message.data.BaseData;
import com.ricky.mq.client.message.data.CallbackData;
import com.ricky.mq.client.message.data.client.ClientBoringMessageData;
import com.ricky.mq.client.message.data.server.ServerBoringMessageData;
import com.ricky.mq.client.message.model.BoringMsgModel;

/**
 * 同步session
 *
 * @author ricky
 * @since 2020.06.18
 */
public class SyncBoringSession extends AbstractBoringSession {

    private static Log LOG = LogFactory.get(SyncBoringSession.class);

    @Override
    public boolean doSend(BoringMessage boringMessage, BoringMsgModel boringMsgModel) {
        BaseData boringData = ClientBoringMessageData.build()
                .setBody(boringMessage.getMessage())
                .setModelMsgName(boringMsgModel.getModelMsgName())
                .setUuid(boringMessage.getUuid());

        String commandCode;
        switch (boringMessage.getMsgType()) {
            case BoringMsgModel.QUEUE:
                commandCode = ClientCommandCode.QUEUE_MESSAGE;
                break;
            case BoringMsgModel.TOPIC:
                commandCode = ClientCommandCode.TOPIC_MESSAGE;
                break;
            default:
                throw new RuntimeException("msgType: " + boringMessage.getMsgType() + "不存在！");
        }

        CallbackData callbackData = WaitOrSendMsgHandler.syncSendData(commandCode, boringData);
        if (callbackData.isSuccess()) {
            ServerBoringMessageData messageData = Convert.convert(ServerBoringMessageData.class, callbackData.getBaseData());
            if (messageData.isSuccess()) {
                LOG.debug("往：" + boringMsgModel.getModelMsgName() + "的消息模型发送消息成功o(*￣︶￣*)o");
                return true;
            }
        }

        return false;
    }
}